const moment = require('moment');
const config = require('../common/config.json')
const sea = require('../common/sea')
var crypto = require('crypto');
var cache = require('memory-cache');
const Manage_MsgType__Manage_AddUserItemC_ = 20001
const Manage_MsgType__Manage_UpdateUserStatusC_ = 20002
const Manage_MsgType__Manage_UpdateGlobalUseTicketC_ = 20003
const Manage_MsgType__Manage_MsgManageResS_ = 20004
const Manage_MsgType__Manage_MsgUserInfoC_ = 20005
const Manage_MsgType__Manage_MsgUserInfoS_ = 10009


class utils {
    async  chenkLogin(id,session) {
        try {
            let sessionserver = cache.get("id_"+id)
            if (session != sessionserver){
                return false
            }
            return true
        } catch (e) {
            console.error("chenkLogin err:",e)
        }
    }
    FormMatDate(time) {
        try {
            let newTime = moment(new Date(JSON.parse(time))).format("YYYY-MM-DD")
            return newTime
        } catch (e) {
            console.error("chenkLogin err:",e)
        }
    }
    FormMatTime(time) {
        try {
            let newTime = moment(new Date(time)).format("YYYY-MM-DD hh:mm:ss")
            return newTime
        } catch (e) {
            console.error("chenkLogin err:",e)
        }
    }

    FormMatTimeNum(time) {
        try {
            let newTime = moment(new Date(time)).format("YYYY-MM-DD hh:mm:ss")
            return newTime
        } catch (e) {
            console.error("chenkLogin err:",e)
        }
    }

    getProBuffById(pid){
        try {
           switch(pid){
               case Manage_MsgType__Manage_AddUserItemC_:
                   return "msg.MsgAddUserItemC";
                   break;
               case Manage_MsgType__Manage_UpdateUserStatusC_:
                   return "msg.MsgUpdateUserStatusC";
                   break;
               case Manage_MsgType__Manage_UpdateGlobalUseTicketC_:
                   return "msg.MsgUpdateTradeStatus";
                   break;
               case Manage_MsgType__Manage_MsgManageResS_:
                   return "msg.MsgManageResS";
                   break;
               case Manage_MsgType__Manage_MsgUserInfoC_:
                   return "msg.MsgUserInfoC";
               case Manage_MsgType__Manage_MsgUserInfoS_:
                   return "msg.MsgUserInfoS";
                   break;
           }
        } catch (e) {
            console.error("getProBuffById err:",e)
        }
    }
    toObjecty (obj){
        try{
            if(typeof obj == 'string'){
                obj = JSON.parse(obj)
            }
            return obj
        }catch(e){
            return null
        }
    }
     getrandomNumber(num) {
        try {
            let code = '';
            for (let i = 0; i < num; i++) {
                code += Math.floor(Math.random() * 10);
            }
            return {code: 200, info: code};
        } catch (e) {
            return {code: 500};
        }
    }
}

module.exports = new utils();



